#include "stdafx.h"

#include "SeaMap_common.h"


Node* loadShpFileAndSetColor(const string& shpFile, float r, float g, float b, float a)
{
	Node* node = NULL;

	string iveFile(shpFile);
	int dotPos = iveFile.rfind('.');
	iveFile.replace(dotPos+1, 3, "ive");
	wstring iveFileW = stringToWString(iveFile);
	boost::filesystem::path ivePath(iveFileW);
	if (boost::filesystem::exists(ivePath))
	{
		node = osgDB::readNodeFile(iveFile);
	}
	else
	{
		wstring shpFileW = stringToWString(shpFile);
		boost::filesystem::path shpPath(shpFileW);
		if (boost::filesystem::exists(shpFileW))
		{
			node = osgDB::readNodeFile(shpFile);
			
			Geode* geode = static_cast<Geode*>(node);
			reprojectOneGeode(geode);
			osgDB::writeNodeFile(*node, iveFile);
		}
	}

	if (node == NULL)
	{
		return NULL;
	}


	Geode* geode = dynamic_cast<Geode*>(node);
	if (geode)
	{
		setColorToOneGeode(geode, r, g, b, a);
	}

	return node;
}


void findEveryGeode(Node* node, vector<Geode*>& geodeList)
{
	Geode* geode = dynamic_cast<Geode*>(node);
	if (geode)
	{
		int size = geodeList.size();
		for (int i = 0; i < size; ++i)
		{
			if (geodeList[i] == geode)
			{
				return;
			}
		}
		geodeList.push_back(geode);
		return;
	}

	Group* group = dynamic_cast<Group*>(node);
	if (group)
	{
		int num = group->getNumChildren();
		for (int i = 0; i < num; ++i)
		{
			findEveryGeode(group->getChild(i), geodeList);
		}
	}
}

void setColorToOneGeode(Geode* geode, float r, float g, float b, float a)
{
	Vec4d color = Vec4d(r, g, b, a);
	Vec4Array* colorArray = new Vec4Array;
	colorArray->push_back(color);

	const Geode::DrawableList& drawableList = geode->getDrawableList();
	Geode::DrawableList::const_iterator it = drawableList.begin();
	for (; it != drawableList.end(); ++it)
	{
		Geometry* geom = dynamic_cast<Geometry*>(it->get());
		if (geom)
		{
			geom->setColorArray(colorArray);
			geom->setColorBinding(Geometry::BIND_OVERALL);
			continue;
		}

		osgText::Text* text = dynamic_cast<osgText::Text*>(it->get());
		if (text)
		{
			text->setBackdropColor(color);
			text->setBackdropType(osgText::Text::OUTLINE);
		}
	}
}

void setColorToEveryGeode(vector<Geode*>& geodeList, float r, float g, float b, float a)
{
	for (unsigned int i = 0; i < geodeList.size(); ++i)
	{
		setColorToOneGeode(geodeList[i], r, g, b, a);
	}
}

void reprojectOneGeode( Geode* geode )
{
	MatrixList matrixList = geode->getWorldMatrices();
	Matrixd worldMatrix = matrixList[0];

	const Geode::DrawableList& drawList = geode->getDrawableList();
	Geode::DrawableList::const_iterator iter = drawList.begin();
	for (; iter != drawList.end(); ++iter)
	{
		Geometry* geom = dynamic_cast<Geometry*>((*iter).get());
		if (geom)
		{
			Vec3Array& vertex = static_cast<Vec3Array&>(*geom->getVertexArray());
			for (unsigned i = 0; i < vertex.size(); ++i)
			{
				double x, y;
				Vec3d tmpVertex = worldMatrix.preMult(vertex[i]);
				SceneCenter::inst()->LLToXY(tmpVertex.x(), tmpVertex.y(), x, y);
				vertex[i].x() = x;
				vertex[i].y() = y;
			}

			geom->dirtyBound();

			continue;
		}

		osgText::Text* text = dynamic_cast<osgText::Text*>((*iter).get());
		if (text)
		{
			double x, y;
			Vec3d pos = text->getPosition();
			Vec3d tmpPos = worldMatrix.preMult(pos);
			SceneCenter::inst()->LLToXY(tmpPos.x(), tmpPos.y(), x, y);
			text->setPosition(Vec3d(x, y, pos.z()));
			text->setCharacterSize(22);
			text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
		}
		
	}
}

void reprojectEveryGeode(vector<Geode*>& geodeList)
{
	int size = geodeList.size();
	for (int i = 0; i < size; ++i)
	{
		reprojectOneGeode(geodeList[i]);
	}
}